package com.itht.baoleme.domain.web;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * web 统一返回对象
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class WebServiceMessage<T> {

    /**
     * 返回的状态码
     */
    private int code;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 返回信息的描述
     */
    private String message;



    public WebServiceMessage(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public WebServiceMessage(int code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    /**
     * 返回数据的成功方法
     * @param data 需要返回给前端的数据
     * @param <T> 数据的类型
     */
    public static <T>WebServiceMessage success(T data) {
        return new WebServiceMessage(WebCodeEnum.SUCCESS.getCode(), data, WebCodeEnum.SUCCESS.getMessage());
    }
    /**
     * 无数据返回的成功方法
     */
    public static <T>WebServiceMessage success() {
        return new WebServiceMessage(WebCodeEnum.SUCCESS.getCode(), WebCodeEnum.SUCCESS.getMessage());
    }

    /**
     * 传一个自定义的message信息
     * @param message 自定义的message信息
     * @return
     */
    public static WebServiceMessage error(String message) {
        return new WebServiceMessage(WebCodeEnum.ERROR.getCode(), message);
    }

    /**
     * 直接报错500 服务异常的信息
     * @return
     */
    public static WebServiceMessage error() {
        return new WebServiceMessage(WebCodeEnum.ERROR.getCode(), WebCodeEnum.ERROR.getMessage());
    }

    /**
     * 传指定枚举状态的错误
     * @param webCodeEnum 枚举状态
     * @return
     */
    public static WebServiceMessage error(WebCodeEnum webCodeEnum) {
        return new WebServiceMessage(webCodeEnum.getCode(), webCodeEnum.getMessage());
    }


}
